<?php
   //sets the system date
   date_default_timezone_set('America/Los_Angeles');
   $gstartdate = date('m/d/Y h:i:s a', time());
   $loadid = date("MDgis");
   $oracle_load_dt = "TO_DATE('".date('d-m-Y', time())."','DD-MM-YYYY')";
   $handle = opendir('./DATA');
   $total_number_of_tables = 0;
   $table_name = "";
   $output     = "";
   $command    = "";
    
   $a=array("truncate table SQL_LOG;");
   array_push($a,"INSERT INTO SQL_LOG(TXT,DATE_TIME,LOAD_ID)VALUES('START LOAD',$oracle_load_dt,$loadid)");

   //gets a list of all files in the current directory
   while (false !== ($entry = readdir($handle))) 
  {
    //only process dbf files
	echo filemtime("./DATA/".$entry) ;
	echo $entry; 
    if((filemtime("./DATA/".$entry) - strtotime("-1 week")) >= 0 )
	{
	  $table_name_raw  = explode(".",$entry);
	  if(strtoupper($table_name_raw [1]) == 'DBF')
      {
        $table_name = $table_name_raw [0];
        $table_name = strtoupper($table_name);
        //this is a special case because of BLOBS
		
		//array_push($a,"INSERT INTO SQL_LOG(TXT,DATE_TIME)VALUES('Loading $table_name',$oracle_load_dt)");
	 
		array_push($a,'@C:\VFPDATA\sql\VFP_'.$table_name.'.sql');
		array_push($a,"INSERT INTO SQL_LOG(TXT,DATE_TIME,LOAD_ID)VALUES('VFP_$table_name:'||to_char((select count(*) from VFP_$table_name)),$oracle_load_dt,'$loadid')");
		array_push($a,"commit");
		
        if($table_name === 'PROJECT')
        {
          $command = "php DataLoaderProjects.php  >> .\logs\load_PROJECTS.log";
        }
        else
        {
           
            //this is were the file is processed
            $command = 'php DataLoader.php '.$table_name.' >> .\logs\load_'.$table_name.'.log';
           
        }
        echo "\r\n Processing:";
		echo $table_name;
		echo "\r\n";
        $startdate = date("h:i:s a", time());
        $output = shell_exec($command);
        $enddate = date("h:i:s a", time());
        echo $output."\r\nTime: Start:".$startdate."\r\nEnd:".$enddate;
        $total_number_of_tables ++;
      }
    }
  }
  $genddate = date('m/d/Y h:i:s a', time());
  echo "\r\nLoaded:".$total_number_of_tables." tables\r\n";
  echo "Start:".$gstartdate."\r\n";
  echo "End:".$genddate ."\r\n"; 
  //print_r($a);
  $text = "";
foreach($a as $key => $value)
{
    $text .=  $value.";\r\n";
}
 $text .= "commit;\r\n quit; \r\n";
$h = fopen('loadSqlData.sql', 'w') or die("Could not open log file.");
fwrite($h, $text) or die("Could not write file!");
fclose($h);

?>